package com.qxkj.youjian.service.impl;

import com.qxkj.youjian.config.MailUtil;
import com.qxkj.youjian.config.RabbitConfiguration;
import com.qxkj.youjian.pojo.MailBean;
import com.qxkj.youjian.pojo.MailForm;
import com.qxkj.youjian.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;
import java.util.Locale;

@Service
public class MailServiceImpl implements MailService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MailUtil mailUtil;

    @Override
    public String sendMsgToMq(String msg) {
        //消息生产
        rabbitTemplate.convertAndSend(RabbitConfiguration.EXCHANGE_A, RabbitConfiguration.ROUTINGKEY_B, msg);
        logger.info("rabbitmq消息已经发送到交换机, 等待交换机接收..." + msg);
        return "success";
    }

    @RabbitListener(queues = RabbitConfiguration.QUEUE_B)
    private void sendEMail(String msg){
        MailBean mailBean = new MailBean();
        mailBean.setSubject("极限云上线测试");
        mailBean.setText(msg);
        //发送邮件
        mailUtil.sendMail(mailBean);
        logger.info("已发送邮件");
    }
}